using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace ScrapeSefariaMongoDBinDotNet
{
    public partial class Form1 : Form
    {
        private void button4_Click(object sender, EventArgs e)
        {
            // https://www.sefaria.org.il/texts
            //
            string add_book;
            string[] mas_ar;
            //
            switch (0)
            {
                case 0:
                    add_book = "";
                    mas_ar = new string[] { "Genesis", "Exodus", "Leviticus" };
                    break;
                case 1:
                    add_book = "Mishnah ";
                    // https://www.sefaria.org.il/texts/Mishnah
                    mas_ar = new string[] { "Berakhot", "Peah", "Demai", "Kilayim", "Sheviit", "Terumot", "Maasrot", "Maaser Sheni", "Challah", "Orlah", "Bikkurim", "Shabbat", "Eruvin", "Pesachim", "Shekalim", "Yoma", "Sukkah", "Beitzah", "Rosh Hashanah", "Taanit", "Megillah", "Moed Katan", "Chagigah", "Yevamot", "Ketubot", "Nedarim", "Nazir", "Sotah", "Gittin", "Kiddushin", "Bava Kamma", "Bava Metzia", "Bava Batra", "Sanhedrin", "Makkot", "Shevuot", "Eduyot", "Avodah Zarah", "*Pirkei Avot", "Horayot", "Zevachim", "Menachot", "Chullin", "Bekhorot", "Arakhin", "Temurah", "Keritot", "Meilah", "Tamid", "Middot", "Kinnim", "Kelim", "Oholot", "Negaim", "Parah", "Tahorot", "Mikvaot", "Niddah", "Makhshirin", "Zavim", "Tevul Yom", "Yadayim", "Oktzin" };
                    break;
                case 2:
                    add_book = ""; // Talmud Bavli
                    //
                    // https://www.sefaria.org.il/texts/Talmud
                    // //a[@class='refLink blockLink']/span[2]
                    mas_ar = new string[] { "Berakhot", "Shabbat", "Eruvin", "Pesachim", "Rosh Hashanah", "Yoma", "Sukkah", "Beitzah", "Taanit", "Megillah", "Moed Katan", "Chagigah", "Yevamot", "Ketubot", "Nedarim", "Nazir", "Sotah", "Gittin", "Kiddushin", "Bava Kamma", "Bava Metzia", "Bava Batra", "Sanhedrin", "Makkot", "Shevuot", "Avodah Zarah", "Horayot", "Zevachim", "Menachot", "Chullin", "Bekhorot", "Arakhin", "Temurah", "Keritot", "Meilah", "Tamid", "Niddah" };
                    break;
                case 3:
                    add_book = "Jerusalem Talmud ";
                    // https://www.sefaria.org.il/texts/Talmud/Yerushalmi
                    mas_ar = new string[] { "Berakhot", "Peah", "Demai", "Kilayim", "Shevi'it", "Terumot", "Ma'asrot", "Ma'aser Sheni", "Hallah", "Orlah", "Bikkurim", "Yevamot", "Sotah", "Ketubot", "Nedarim", "Nazir", "Gittin", "Kiddushin", "Bava Kamma", "Bava Metsia", "Bava Batra", "Sanhedrin", "Shevuot", "Avodah Zarah", "Makkot", "Horayot", "Shabbat", "Pesachim", "Yoma", "Shekalim", "Sukkah", "Rosh Hashanah", "Beitzah", "Ta'anit", "Megillah", "Chagigah", "Moed Kattan" };
                    break;
                default:
                    throw new Exception();
            }
            //
            List<book_info> lbi = new List<book_info>();
            JArray ja = new JArray();
            foreach (string mas in mas_ar)
            {
                string title = "";
                Match m = Regex.Match(mas, @"^[*](.+)$");
                if (m.Success)
                {
                    title = m.Groups[1].Value;
                }
                else
                {
                    title = add_book + mas;
                }
                title = "" + title; // Rambam on Mishnah Beitzah, Rashi on Beitzah, Steinsaltz on Beitzah
                string p = ""; //  ", priority: 3";
                string re_ar_str = parse_talmud_title(title, p);

                //JObject jt = new JObject();
                //jt.add


                book_info bi = new book_info(title, null, re_ar_str.Split(',').Select(x => int.Parse(x)).ToArray());
                string json = JsonConvert.SerializeObject(bi);
                ja.Add(json);
                lbi.Add(bi);
                Console.WriteLine(title + ": " + re_ar_str);
            }
            book_info[] books_array = lbi.ToArray();
            // todo: serialize and save and read.
        }

        private static string parse_talmud_title(string title, string p)
        {
            string xx = "{ title: { $regex: \"^" + title + "$\" }, language: 'he'" + p + " }"; //, language: 'he', versionTitle: 'Tanach with Nikkud' }";;
            FilterDefinition<BsonDocument> filter = xx;
            BsonDocument x = (BsonDocument)texts_collection.Find(filter).FirstOrDefault();
            if (x == null) return "<null>";
            BsonArray chapters_ar = (BsonArray)x["chapter"];
            string chaps_str = "";
            for (int i = 0; i < chapters_ar.Count; i++)
            {
                BsonArray sec_ar = (BsonArray)chapters_ar[i];
                int n_sec = sec_ar.Count;
                if (chaps_str != "") chaps_str += ",";
                chaps_str += n_sec;

                foreach (BsonValue bv in sec_ar)
                {
                    string txt = (string)bv;
                }
            }
            //chaps_str = $"[ {chaps_str}]";
            return chaps_str;
        }
    }
}
